# koroFileHeader at Yungoal acer
# Create: 2021-04-25 15:06:24
# LastEdit: 2021-07-07 11:11:00
"""说明暂无"""
__author__ = '749B'

import unittest
import io
import sys
import textwrap

if __name__ == '__main__':
    import os
    sys.path.append(os.getcwd())
# pylint: disable = import-error
from utils.tools import is_dev_environment
from utils.tools import remove_empty
from utils.tools import id_to_dict
from utils.tools import init_printer


class IsDevTestCase(unittest.TestCase):
    
    def test_is_dev_environment(self):
        res = is_dev_environment()
        self.assertTrue(res)
        

class RemoveEmptyTestCase(unittest.TestCase):

    def test_remove_empty(self):
        befor = textwrap.dedent("""\
            Line 1
            Line 2
            {empty}
            Line -1
            """).format(
            empty='', 
            )
        after = textwrap.dedent("""\
            Line 1
            Line 2
            Line -1""")  # 最后不能有换行符
        self.assertEqual(remove_empty(befor), after)


class IdToDictTestCase(unittest.TestCase):

    def test_id_to_dict(self):
        s = "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" \
            "/resourceGroups/Group01" \
            "/providers/Microsoft.Compute" \
            "/virtualMachines/Node01"
        d = {
            'subscriptions': 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx', 
            'resourceGroups': 'Group01',
            'providers': 'Microsoft.Compute',
            'virtualMachines': 'Node01',
            }
        # 把Key转小写
        d = {k.lower(): v for k, v in d.items()}
        self.assertDictEqual(id_to_dict(s), d)

    def test_not_start_with_sep(self):
        """没有分隔符开头的情况"""
        s = "subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" \
            "/resourceGroups/Group01" \
            "/providers/Microsoft.Compute" \
            "/virtualMachines/Node01"
        d = {
            'subscriptions': 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx', 
            'resourceGroups': 'Group01',
            'providers': 'Microsoft.Compute',
            'virtualMachines': 'Node01',
            }
        # 把Key转小写
        d = {k.lower(): v for k, v in d.items()}
        self.assertDictEqual(id_to_dict(s), d)

    def test_comma_sep(self):
        """可以用别的分隔符，测试下逗号"""
        s = "subscriptions,xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" \
            ",resourceGroups,Group01" \
            ",providers,Microsoft.Compute" \
            ",virtualMachines,Node01"
        d = {
            'subscriptions': 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx', 
            'resourceGroups': 'Group01',
            'providers': 'Microsoft.Compute',
            'virtualMachines': 'Node01',
            }
        # 把Key转小写
        d = {k.lower(): v for k, v in d.items()}
        self.assertDictEqual(id_to_dict(s, ','), d)

    def test_strings_sep(self):
        """分隔符的要求是字符串，也就是可以是一串字符包括使用中文"""
        s = "| 分隔符 |subscriptions| 分隔符 |xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" \
            "| 分隔符 |resourceGroups| 分隔符 |Group01" \
            "| 分隔符 |providers| 分隔符 |Microsoft.Compute" \
            "| 分隔符 |virtualMachines| 分隔符 |Node01"
        d = {
            'subscriptions': 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx', 
            'resourceGroups': 'Group01',
            'providers': 'Microsoft.Compute',
            'virtualMachines': 'Node01',
            }
        # 把Key转小写
        d = {k.lower(): v for k, v in d.items()}
        self.assertDictEqual(id_to_dict(s, '| 分隔符 |'), d)


class InitPrinterTestCase(unittest.TestCase):

    def setUp(self) -> None:
        self.stdout = sys.stdout
        # 输出重定向
        sys.stdout = io.StringIO()

    def tearDown(self) -> None:
        sys.stdout = self.stdout

    def test_init_printer(self):
        msg = 'Test Message. 测试用消息。'
        printer = init_printer(msg)
        printer(msg)
        self.assertEqual(sys.stdout.getvalue(), msg + '\n')


if __name__ == '__main__':
    unittest.main()

